<template>
	 <view>
		 
	 </view>
</template>

<script>
	'use strict';
	import { getSellerPayConfigBySellerId } from '@/api/apiServe'
	var app = getApp();
	var https_1 = require('./../../../../api/https.js');
	
	export default {
		components: {
		},
		data() {
			return {
				sellerNo :app.globalData.sellerNo,
				gunDetailOptionsStr:'',
				getRechargeResultNum:1
			};
		},
		computed: {
			
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			let that = this
			this.orderNo = options.orderNo
			this.status = options.status 
			
			let gunDetailOptions = uni.getStorageSync('gunDetailOptions') || {}
			
			let gunDetailOptionsStr = this.getOptionsQuery(gunDetailOptions)
			this.gunDetailOptionsStr = gunDetailOptionsStr
			uni.removeStorageSync('gunDetailOptions')
			
			if(this.status == 'fail'){
				uni.showModal({
					title: '提示',
					content: '用户取消支付',
					showCancel:false,
					success: function (res) {
						uni.switchTab({
							url: '/pages/index/index',
							success(){
								setTimeout(()=>{
									uni.redirectTo({
										url:`/homeSub/pages/terminal/gunDetail/gunDetail${that.gunDetailOptionsStr}`,
									})
								},500)
							}
						});
					}
				});
			}else{
				this.getRechargeResult()
			}
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
			
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {
			
		},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {
			
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function () {},
		methods:{
			getOptionsQuery(options) {
				var query = options || {};
				var str = [];
				var queryStr = '';
				for (var p in query) {
					if (query[p]) {
						str.push(p + "=" + query[p]);
					}
				}
				queryStr = str.join("&");
				if (queryStr) {
					return '?' + queryStr;
				}
				return queryStr;
			},
			//桔禾付 查询充值结果
			getRechargeResult: function() {
				var that = this;
				this.rechargeLoading = true
				uni.showLoading({title:'充值中..',mask:true})
				this.disposableOrderResultQuery();
				this.Interval = setInterval(()=>{
					if (that.getRechargeResultNum > 10) {
						uni.hideLoading()
						clearInterval(that.Interval);
						that.disposableCancelRecharge();
					} else {
						that.disposableOrderResultQuery();
					}
				}, 3000);
			},
			//桔禾付 启动充电-充完即退-查询支付结果
			disposableOrderResultQuery: function () {
				var that = this;
				var num = this.getRechargeResultNum + 1;
				this.getRechargeResultNum = num
				let apiM = https_1.queryPayment
				if(that.isZshAppInH5()){
					apiM = https_1.CMPayOrderQuery
				}
				
				apiM({
						orderNo: this.orderNo
					})
					.then(function(res) {
						if (res.code === 0) {
							uni.hideLoading()
							clearInterval(that.Interval);
							that.getRechargeResultNum = 1
							that.enterDisposableStartCharge()
						}else if( res.code == 1001){
							clearInterval(that.Interval);
							that.getRechargeResultNum = 1
							uni.hideLoading()
							uni.showModal({
								title: '消息提示',
								content: '用户已取消支付',
								showCancel:false,
								success: function (res) {
									uni.switchTab({
										url: '/pages/index/index',
										success(){
											setTimeout(()=>{
												uni.redirectTo({
													url:`/homeSub/pages/terminal/gunDetail/gunDetail${that.gunDetailOptionsStr}`,
												})
											},500)
										}
									});
								}
							});
						}
					});
			},
			//启动充电-充完即退-微信支付成功后鉴权
			enterDisposableStartCharge : function () {
				var that=this
				let payIngData = uni.getStorageSync('payIngData')
				// uni.removeStorageSync('payIngData')
				var str = payIngData
				console.log(JSON.stringify(str),'8888888')
				
				uni.switchTab({
					url: '/pages/index/index',
					success(){
						setTimeout(()=>{
							uni.navigateTo({
								url: '/homeSub/pages/startCharging/startCharging?parameter=' + JSON.stringify(str)
							});
						},500)
					}
				});
			},
			
			//启动充电-充完即退-微信取消充值 -- 支付后
			disposableCancelRecharge: function() {
				let that= this
				uni.showModal({
					title: '消息提示',
					content: '网络异常，如果产生多次扣费，请联系客服人员退款',
					showCancel:false,
					success: function (res) {
						uni.switchTab({
							url: '/pages/index/index',
							success(){
								setTimeout(()=>{
									uni.redirectTo({
										url:`/homeSub/pages/terminal/gunDetail/gunDetail${that.gunDetailOptionsStr}`,
									})
								},500)
							}
						});
					}
				});
				let apiM = https_1.disposableCancelRecharge
				if(that.isZshAppInH5()){
					apiM = https_1.CMPayCancelRecharge
				}
				apiM({
					orderNo: this.orderNo
				})
				.then(()=>{
					// uni.switchTab({
					// 	url: '/pages/index/index',
					// 	success(){
					// 		setTimeout(()=>{
					// 			uni.redirectTo({
					// 				url:`/homeSub/pages/terminal/gunDetail/gunDetail${that.gunDetailOptionsStr}`,
					// 			})
					// 		},500)
					// 	}
					// });
				});
			},
		}
	};
</script>
<style lang="less">

</style>
